package com.netease.nim.uikit.util;

import android.content.Context;
import android.media.AudioManager;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;

import com.netease.nimlib.j.b;
import com.netease.nimlib.r.m;
import com.netease.nimlib.r.s;
import com.netease.nimlib.sdk.media.record.AudioRecorder;
import com.netease.nimlib.sdk.media.record.IAudioRecordCallback;
import com.netease.nimlib.sdk.media.record.RecordType;
import com.netease.share.media.a;
import com.netease.share.media.c;

import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean;

public class AudioRecorderS {

    public static final int DEFAULT_MAX_AUDIO_RECORD_TIME_SECOND = 120;
    private static final int MSG_START_RECORD = 1;
    private static final int MSG_STOP_RECORD = 2;
    private static final int MSG_END_RECORD = 3;
    private static final int RECORD_FAILED = 1;
    private static final int RECORD_READY = 2;
    private static final int RECORD_START = 3;
    private static final int RECORD_SUCCESS = 4;
    private static final int RECORD_CANCELED = 5;
    private static final String TAG = "AudioRecordManager";
    private a mAudioRecorder;
    private AudioManager audioManager;
    private Context context;
    private int networkClass = 0;
    private File audioFile;
    private RecordType recordType;
    private int maxDuration;
    private AtomicBoolean isRecording = new AtomicBoolean(false);
    private AtomicBoolean cancelRecord = new AtomicBoolean(false);
    private IAudioRecordCallback cb;
    private RecordHandler mHandler;
    private Handler mEventHandler = new Handler(Looper.getMainLooper());
    private HandlerThread handlerThread;
    private c infoListener = new c() {
        public void onInfo(int var1, int var2, int var3) {
            if (var2 == 2) {
                handleEndRecord(false, 0);
            } else if (var2 == 1) {
                mHandler.post(new Runnable() {
                    public void run() {
                        isRecording.set(false);
                    }
                });
                handleReachedMaxRecordTime(var3);
            } else if (var2 == 3) {
            }

        }
    };

    public AudioRecorderS(Context var1, RecordType var2, int var3, IAudioRecordCallback var4) {
        this.context = var1.getApplicationContext();
        this.recordType = var2;
        if (var3 <= 0) {
            this.maxDuration = 120;
        } else {
            this.maxDuration = var3;
        }

        this.cb = var4;
        this.handlerThread = new HandlerThread("audio_recorder");
        this.handlerThread.start();
        this.mHandler = new RecordHandler(this.handlerThread.getLooper());
    }

    public void startRecord() {
        this.mHandler.removeMessages(1);
        this.mHandler.obtainMessage(1).sendToTarget();
    }

    private void onStartRecord() {
        this.audioManager.requestAudioFocus((AudioManager.OnAudioFocusChangeListener)null, 0, 2);

    }

    public void completeRecord(boolean var1) {
        Message var2 = this.mHandler.obtainMessage(2);
        var2.obj = var1;
        var2.sendToTarget();
    }

    private void onCompleteRecord(boolean var1) {
        if (this.isRecording.get()) {
            this.cancelRecord.set(var1);
            this.audioManager.abandonAudioFocus((AudioManager.OnAudioFocusChangeListener)null);

            try {
                if (this.mAudioRecorder != null) {
                    this.mAudioRecorder.b();
                    this.onHandleEndRecord(true, this.mAudioRecorder.d());
                    this.mAudioRecorder = null;
                }
            } catch (Exception var3) {
                var3.printStackTrace();
            }

        }
    }

    public void destroyAudioRecorder() {
        if (this.mHandler != null) {
            this.mHandler.removeCallbacksAndMessages((Object)null);
        }

        if (this.handlerThread != null && this.handlerThread.isAlive()) {
            Looper var1 = this.handlerThread.getLooper();
            var1.quit();
        }

    }

    public boolean isRecording() {
        return this.isRecording.get();
    }

    public void handleEndRecord(boolean var1, int var2) {
        Message var3 = this.mHandler.obtainMessage(3);
        var3.obj = var1;
        var3.arg1 = var2;
        var3.sendToTarget();
    }

    private void onHandleEndRecord(boolean var1, final int var2) {
        if (this.cancelRecord.get()) {
            com.netease.nimlib.l.a.c.a.c(this.audioFile.getAbsolutePath());
            this.callBackRecordState(5);
        } else if (!var1) {
            com.netease.nimlib.l.a.c.a.c(this.audioFile.getAbsolutePath());
            this.callBackRecordState(1);
        } else if (this.audioFile != null && this.audioFile.exists() && this.audioFile.length() > 0L) {
            this.mEventHandler.post(new Runnable() {
                public void run() {
                    cb.onRecordSuccess(audioFile, (long)var2, recordType);
                }
            });
        } else {
            this.callBackRecordState(1);
        }

        this.isRecording.set(false);
    }

    private void callBackRecordState(final int var1) {
        this.mEventHandler.post(new Runnable() {
            public void run() {
                switch(var1) {
                    case 1:
                        cb.onRecordFail();
                        break;
                    case 2:
                        cb.onRecordReady();
                        break;
                    case 3:
                        cb.onRecordStart(audioFile, recordType);
                    case 4:
                    default:
                        break;
                    case 5:
                        cb.onRecordCancel();
                }

            }
        });
    }

    public int getCurrentRecordMaxAmplitude() {
        return this.mAudioRecorder != null ? this.mAudioRecorder.a() : 0;
    }

    private void handleReachedMaxRecordTime(int var1) {
        this.cb.onRecordReachedMaxTime(var1);
    }

    private class RecordHandler extends Handler {
        public RecordHandler(Looper var2) {
            super(var2);
        }

        public void handleMessage(Message var1) {
            switch(var1.what) {
                case 1:
                    onStartRecord();
                    break;
                case 2:
                    boolean var2 = (Boolean)var1.obj;
                    onCompleteRecord(var2);
                    break;
                case 3:
                    boolean var3 = (Boolean)var1.obj;
                    int var4 = var1.arg1;
                    onHandleEndRecord(var3, var4);
            }

        }
    }
}
